What is resolve-from?
The resolve-from npm package is a module that allows you to resolve the path of a module like require.resolve() but from a given path. It is useful for situations where you need to resolve modules from a different directory than the current file's directory.
What are resolve-from's main functionalities?
Resolve the path of a module from a given path
This feature allows you to resolve the path of a module from a specific directory. You provide the directory as the first argument and the module name as the second argument. It returns the resolved path to the module.
const resolveFrom = require('resolve-from');
const modulePath = resolveFrom('/some/path', 'some-module');
Silently resolve the path of a module or return null
This feature is similar to the first, but it will return null instead of throwing an error if the module cannot be found. This is useful for cases where you want to check for a module's existence without causing an exception.
const resolveFrom = require('resolve-from');
const modulePath = resolveFrom.silent('/some/path', 'non-existent-module');
Other packages similar to resolve-from
resolve
The 'resolve' package is similar to 'resolve-from' in that it helps to resolve the path of a module. However, 'resolve' provides more options and flexibility, such as the ability to customize the resolution algorithm or to resolve based on the package.json's 'main' field.
enhanced-resolve
The 'enhanced-resolve' package is used by webpack and offers a highly configurable resolution mechanism that can resolve files, directories, and modules. It is more complex and feature-rich compared to 'resolve-from', which is simpler and more straightforward.
browser-resolve
The 'browser-resolve' package is designed to resolve modules for browser environments, taking into account the 'browser' field in package.json. It is similar to 'resolve-from' but is specifically tailored for browser-based module resolution.
resolve-from
Resolve the path of a module like require.resolve()
but from a given path
Install
$ npm install resolve-from
Usage
const resolveFrom = require('resolve-from');
resolveFrom('foo', './bar');
API
resolveFrom(fromDir, moduleId)
Like require()
, throws when the module can't be found.
resolveFrom.silent(fromDir, moduleId)
Returns null
instead of throwing when the module can't be found.
fromDir
Type: string
Directory to resolve from.
moduleId
Type: string
What you would use in require()
.
Tip
Create a partial using a bound function if you want to resolve from the same fromDir
multiple times:
const resolveFromFoo = resolveFrom.bind(null, 'foo');
resolveFromFoo('./bar');
resolveFromFoo('./baz');
Related
- resolve-cwd - Resolve the path of a module from the current working directory
- import-from - Import a module from a given path
- import-cwd - Import a module from the current working directory
- resolve-pkg - Resolve the path of a package regardless of it having an entry point
- import-lazy - Import a module lazily
- resolve-global - Resolve the path of a globally installed module
License
MIT © Sindre Sorhus